using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DomainModel.Entities;
using DomainModel.Abstract;

namespace WebSite.Controllers
{
    // The controller for dealing with views regarding the creation and
    // editing of job types and the ability to associate questions with them.
    [Authorize(Roles = "HiringManager")]
    public class PositionsManagementController : Controller
    {
        // The database abstractions for the jobs, questions, questionnaireEntries, and positions.
        private IPositionsRepository positionsRepository;
        private IJobsRepository jobsRepository;

        // Constructor, it attachs the jobs repository, the questions repository, 
        // the questionnaireEntry repository, and the positions repository to the 
        // controller.
        public PositionsManagementController(IPositionsRepository positionsRepository,
                                             IJobsRepository      jobsRepository)
        {
            this.positionsRepository    = positionsRepository;
            this.jobsRepository         = jobsRepository;
            //this.storeID = 0;

            TempData["error"] = "";
            TempData["store"] = 0;
        }

        // This is just a wrapper for the query to get a job given an id.
        private Position GetPosition(int positionID)
        {
            Position position = (from p in positionsRepository.Positions
                                 where p.PositionID == positionID
                                 select p).First();
            return position;
        }

        private Position GetPositionByJobAndStoreID(int jobID, int storeID)
        {
            List<Position> positions = GetPositionsByStoreID(storeID);
            Position position = (from p in positions
                                 where p.JobID == jobID
                                 select p).First();
            return position;
        }

        private Job GetJob(int jobID)
        {
            Job job = (from j in jobsRepository.Jobs
                       where j.JobID == jobID
                       select j).First();
            return job;
        }

        //UNSURE*******

        private List<Position> GetPositionsByStoreID(int storeID)
        {
            //Position position = GetPosition(;
            List<Position> positions = new List<Position>();
            foreach (Position p in positionsRepository.Positions)
            {
                if (p.StoreID == storeID)
                {
                    positions.Add(p);
                }
            }

            return positions;
        }

        //**************

        // Get a list of Questions that are attached to a job
        // given a jobID.
        public List<Job> GetJobs(int storeID)
        {
            //List<Position> positions = GetPositions(storeID);
            List<Job> jobs = new List<Job>();
            foreach (int jid in jobsRepository.Jobs.Select(od => od.JobID))
            {
                foreach (int pid in positionsRepository.Positions.Select(pd => pd.PositionID))
                {
                    Position pos = GetPosition(pid);
                    Job job = GetJob(jid);
                    if ((pos.StoreID == storeID) && (pos.JobID == job.JobID))
                        jobs.Add(job);
                }
            }

            return jobs;
        }

        public List<Job> GetAllOtherJobs(int storeID)
        {
            List<Job> jobs = new List<Job>();
            List<Job> currentJobs = GetJobs(storeID);
            foreach (Job j in jobsRepository.Jobs)
            {
                if (!currentJobs.Contains(j))
                    jobs.Add(j);
            }

            return jobs;
        }
        //public ViewResult Index()
        //{
        //    return View(GetJobs(this.storeID));
        //}

        // Provides list of jobs in repository as model to Index view.
        public ViewResult Index(int storeID)
        {
            TempData["store"] = storeID;
            return View(GetJobs((int)TempData["store"]));
        }

        // Instantiates a new job and passes it to the Edit view.
        public ActionResult Create(int storeID)
        {
            TempData["store"] = storeID;
            return View("Edit", GetAllOtherJobs(storeID));
        }

        // Edit command for http get, returns a job given an id
        // and passes the job to the view.
        //[AcceptVerbs(HttpVerbs.Get)]
        //public ViewResult Edit(int positionID)
        //{
        //    Position position = GetPosition(positionID);

        //    return View(position);
        //}

        // Edit command for post, places a job from the view into
        // the repository.
        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Edit(int jobID)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        //positionsRepository.SavePosition(position);
        //        return RedirectToAction("Index");
        //    }
        //    else
        //    {
        //        return View();
        //    }
        //}

        public ActionResult Add(int jobID, int storeID)
        {
            if (ModelState.IsValid)
            {
                Position position = new Position();
                position.PositionID = 0;
                position.JobID = jobID;
                position.StoreID = storeID;
                positionsRepository.SavePosition(position);
                return RedirectToAction("Index", new { storeID } );
            }
            else
            {
                return View();
            }
        }

        // The Http-Get for delete job. Provide job to be deleted
        // to the view.
        [ActionName("Delete")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete_GET(int jobID, int storeID)
        {
            TempData["store"] = storeID;
            return View(GetJob(jobID));
        }

        // This deletes a job from the repository given a
        // id from the view. Also deletes any bridge entries between
        // the job and the questions. It will not delete anything if
        // there are any answeredQuestions pointing at the attached
        // questions or Positions pointing at the job.
        //[ActionName("Delete")]
        //[AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult Delete_POST(int jobID, int storeID)
        {
            Position position = GetPositionByJobAndStoreID(jobID, storeID);

            positionsRepository.DeletePosition(position);

            return RedirectToAction("Index", new { storeID });
        }

        public ViewResult List()
        {
            return View(positionsRepository.Positions.ToList());
        }
    }
}
